# -*- coding: utf-8 -*-
# 高频策略
import traceback
from tool.gongju import *
from tool.zhibiao import ma_z

HEYUEZF = {}
HEYUEPM = {}
PJZDF = 0  # 平均涨跌幅


def zhangdiefu_dhy(cl):
    """
    开仓逻辑：
       全市场整体关联，结合市场情绪做单，选出30个或者40个有代表性的品种，计算出他们的平均涨跌幅，在30分钟周期下，就是平均涨幅1%，平均跌幅-1%，
       作为风控；如果品种涨跌幅达不到风控要求，就多开一只当天涨幅最大的，空开一只当天跌幅最大的，形成1比1 对冲，如果行情出现风控的要求，
       30或者40个品种平均涨幅达到1%，那就多单开涨幅最大的3个品种，空开跌幅最大的1个品种，平均跌幅达到-1%就开空跌幅最大的3个品种，多1个不变，
       涨跌停的过滤掉，用网格在3分钟周期下开单，胜率很高，
    平仓逻辑：
        出场1：百分之2的吊灯止损
        收盘清仓
    """
    # 数据初始化，
    Datetime = cl.klines[0].datetime.values
    # Close = cl.klines[0].close.values
    Open = cl.klines[0].open.values
    # High = cl.klines[0].high.values
    # Low = cl.klines[0].low.values
    # volume = cl.klines[0].volume.values
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    chicang = cl.openingdata['kaicangzhuangtai']
    chicangshuliang = cl.openingdata['kaicangshuliang'] * chicang
    shijian = int(time_to_datetime(Datetime[-1]).time().strftime("%H%M%S"))  # 当前时间
    zhbh = cl.jiaoyishezhi[0][0]  # 开仓账户编号
    heyue = cl.jiaoyishezhi[3][0]

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 初次开仓数量
    zy = cl.jiaoyishezhi[5][1]  # 止盈吊灯百分比
    涨幅阀值 = cl.jiaoyishezhi[5][2]
    开仓时间 = cl.jiaoyishezhi[5][3]
    qingcangshijian = [145500, 225500]  # 清仓时间

    if 'hydx' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl.sjb['hydx'] = cl.zhanghu[zhbh].get_zuixiaobiandong(cl.jiaoyishezhi[3][0])   # 最小变动单位
        HEYUEZF[heyue] = 0

    # 涨幅计算
    tick = DuquRedis_HQ(heyue)
    zspj = float(tick['PreClosePrice'])
    zf = round((float(tick['LastPrice']) - zspj)/zspj*100, 3)
    HEYUEZF[heyue] = zf

    # 获取排名
    pm = HEYUEPM[heyue]

    # if cl.shangcishijian != Datetime[-1]:
    #     # DKX计算
    #     pass
    if PJZDF > 涨幅阀值 and shijian >= 开仓时间:
        if (pm == 36 or pm == 35) and chicang == 0:
            Buy(ss, Open[-1], canshu)
        else:
            if pm == 0:
                SellShort(ss, Open[-1], canshu)
    else:
        if PJZDF < -涨幅阀值 and shijian >= 开仓时间:
            if (pm == 0 or pm == 1) and chicang == 0:
                SellShort(ss, Open[-1], canshu)
            else:
                if pm == 36:
                    Buy(ss, Open[-1], canshu)

    paimingzhongxian = 0
    if pm == 35 or pm == 36:
        paimingzhongxian = 88
    else:
        if pm == 0 or pm == 1:
            paimingzhongxian = 66

    return zf, pm, paimingzhongxian, 0


def zhangdiefu_px(cl):
    """
    根据计算的单合约涨幅排序，放到单合约后面
    """
    # 数据初始化，
    Datetime = cl.klines[0].datetime.values
    # Close = cl.klines[0].close.values
    Open = cl.klines[0].open.values
    # High = cl.klines[0].high.values
    # Low = cl.klines[0].low.values
    # volume = cl.klines[0].volume.values
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    chicang = cl.openingdata['kaicangzhuangtai']
    chicangshuliang = cl.openingdata['kaicangshuliang'] * chicang
    shijian = int(time_to_datetime(Datetime[-1]).time().strftime("%H%M%S"))  # 当前时间
    zhbh = cl.jiaoyishezhi[0][0]  # 开仓账户编号
    heyue = cl.jiaoyishezhi[3][0]

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 初次开仓数量
    by = cl.jiaoyishezhi[5][1]  # 备用

    if 'hydx' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl.sjb['hydx'] = cl.zhanghu[zhbh].get_zuixiaobiandong(cl.jiaoyishezhi[3][0])   # 最小变动单位
        cl.sjb['合约涨幅'] = HEYUEZF
        cl.sjb['合约排名'] = HEYUEPM

    # 涨幅计算
    pxhouzidian = sorted(HEYUEZF.items(), key=lambda zd: zd[1], reverse=False)  # 字典进行排序
    lsbl = 0
    zongzhangdiefu = 0  # 总涨跌幅
    for k in pxhouzidian:
        HEYUEPM[k[0]] = lsbl  # 存入排名序号
        zongzhangdiefu = zongzhangdiefu + k[1]
        lsbl = lsbl + 1
    global PJZDF
    PJZDF = round(zongzhangdiefu/lsbl, 3)
    cl.sjb['合约涨幅'] = HEYUEZF
    cl.sjb['合约排名'] = HEYUEPM

    return PJZDF, lsbl, 0, 0